package com.yoomiito.app.base;

import android.os.Bundle;
import androidx.annotation.Nullable;

import com.yoomiito.app.model.bean.UserInfo;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.EventMessage;
import com.yoomiito.app.utils.L;
import com.yoomiito.app.utils.LoginUtils;
import com.yoomiito.app.utils.YMToast;
import com.yoomiito.app.widget.TipDialog;
import com.yoomiito.app.widget.dialog.LoadDialog;
import com.yoomiito.app.utils.UserInfoConfig;
import com.yoomiito.app.widget.dialog.NoNetDialog;
import com.yoomiito.app.widget.state.StatusManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.droidlover.xdroidmvp.mvp.IPresent;
import cn.droidlover.xdroidmvp.mvp.XLazyFragment;
import cn.droidlover.xdroidmvp.net.ApiException;

public abstract class BaseLazyFragment<P extends IPresent> extends XLazyFragment<P> {

    public StatusManager mStatusManager;
    private LoadDialog loadDialog;
    private NoNetDialog mNoNetDialog;

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        L.i("解除绑定消息");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }
    private void showNewDialog(boolean show){
        if (mNoNetDialog==null){
            mNoNetDialog = new NoNetDialog(context);
        }
        if (show){
            mNoNetDialog.show();
//            L.i("弹窗:"+mNoNetDialog.isShowing());
//            L.i("弹窗1:"+!mNoNetDialog.isShowing());
//            if (!mNoNetDialog.isShowing()) {
//                L.i("准备显示");
//                mNoNetDialog.show();
//                L.i("显示了");
//            }
        }else {
            mNoNetDialog.dismiss();
        }
    }
    /**
     * eventbus事件处理
     *
     * @param eventMessage
     */
    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void eventMessage(EventMessage eventMessage){
        L.i("BaseFragment收到消息："+eventMessage.getEventType());
        if (AllCode.NET_DIALOG_SHOW.equals(eventMessage.getEventType())){
            showNewDialog(true);
            L.i("BaseFragment收到消息：显示网络弹窗");
        }
        if (AllCode.NET_DIALOG_DISMISS.equals(eventMessage.getEventType())){
            showNewDialog(false);
            L.i("BaseFragment收到消息：取消网络弹窗");
        }
    }

    /**
     * 显示选择性的对话框
     *
     * @param content
     */
    public void showSelectDialog(String tipText, String content, String sureText, String cancelText, TipDialog.Dialogcallback dialogcallback) {
        TipDialog dialog = new TipDialog(context, TipDialog.TYPE_SELECT);
        dialog.setTipText(tipText);
        dialog.setTipText(content);
        dialog.setSureText(sureText);
        dialog.setcancleText(cancelText);
        dialog.setDialogCallback(dialogcallback);
        dialog.show();
    }

    public void showLoading() {
        if (loadDialog == null) {
            loadDialog = new LoadDialog(context);
        }
        loadDialog.show();
    }

    public void dismissLoading() {
        if (loadDialog != null && loadDialog.isShowing()) {
            loadDialog.dismiss();
        }
    }

    public void setUserInfo(UserInfo userInfo) {
        dismissLoading();
        if (userInfo==null){
//            AllUtils.toLogin(context, AllCode.LoginIn);
            LoginUtils.INSTANCE.toOneKeyLogin((BaseActivity)context);
            context.finish();
        }else {
            App.mAppConfig.mCurrentUser.mUserInfo = userInfo;
            UserInfoConfig.updateRoleId(userInfo);
        }
    }

    @Override
    public void handleError(ApiException e) {
        switch (e.getCode()) {
//            case ApiException.UNKNOWN:
//            case ApiException.NETWORD_ERROR:
//            case ApiException.TIME_OUT_ERROR:
//            case ApiException.VERIFICATION_CODE_TYPE:
//            case ApiException.WEIXIN_BANGDING_TYPE:
//            case ApiException.INVITATION_CODE_TYPE:
//            case ApiException.INPUT_INVITATION_CODE_TYPE:
//            case ApiException.YES_INVITE_TYPE:
//            case ApiException.WEIXIN_AUTHORIZATION_TYPE:
//            case ApiException.AUTHORIZATION_DATA_TYPE:
//                getvDelegate().toastShort(e.getMsg());
//                break;
            case ApiException.TOKEN_OUT_DATA:
//                AllUtils.toLogin(context, AllCode.LoginIn);
                LoginUtils.INSTANCE.toOneKeyLogin((BaseActivity) context);
                break;
            case 4001://快抢空数据
                break;
            default:
                YMToast.showText(e.getMsg());
        }
    }

}
